package com.yxhl.zoume.core.func.pay.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.pay.model.ExpressCarBill;
import com.yxhl.zoume.core.func.pay.model.ExpressCarBillDetail;
import com.yxhl.zoume.core.func.pay.view.ExpressCarBillDetailView;
import com.yxhl.zoume.data.http.model.expresscar.BasicFeeDetail;
import com.yxhl.zoume.data.http.model.expresscar.ExpressCarBillInfo;
import com.yxhl.zoume.data.http.model.expresscar.MainFeeDetail;
import com.yxhl.zoume.data.http.model.expresscar.OtherFeeDetail;
import com.yxhl.zoume.data.http.rest.param.expresscar.ExpressCarBillDetailParam;
import com.yxhl.zoume.data.http.rest.response.expresscar.ExpressCarBillDetailResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.pay.ExpressCarBillDetailUseCase;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by vhawk on 12/22/16.
 */
public class ExpressCarBillDetailPresenter extends BasePresenter {

    private ExpressCarBillDetailView view;

    @Inject
    ExpressCarBillDetailUseCase useCase;

    @Inject
    public ExpressCarBillDetailPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (ExpressCarBillDetailView) view;
    }

    public void fetchBillDetail(String orderSerial, String totalFee, String discountFee) {
        ExpressCarBillDetailParam param = new ExpressCarBillDetailParam();
        param.setTotalFee(totalFee)
                .setOrderSerial(orderSerial)
                .setDiscountFee(discountFee)
                .setMobile(getMobile())
                .setToken(getToken());

        useCase.execute(param,new ZMSubscriber<ExpressCarBillDetailResponse>() {
                    @Override
                    public void onNext(ExpressCarBillDetailResponse response) {
                        super.onNext(response);
                        if (response.isSucc()) {
                            ExpressCarBill expressCarBill = generateExpressCarBill(response.getExpressCarBillInfo());
                            view.handleBillDetailSucceed(expressCarBill);
                        } else {
                            view.handleBillDetailFailure(response.getResultMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.handleBillDetailNetError();
                    }
                });

    }

    private ExpressCarBill generateExpressCarBill(ExpressCarBillInfo expressCarBillInfo) {

        ExpressCarBill bill = new ExpressCarBill();
        bill.setTotalFee(expressCarBillInfo.getTotalFee())
                .setDistance(expressCarBillInfo.getRunKm())
                .setTimeDuring(expressCarBillInfo.getRunMin());

        List<ExpressCarBillDetail> carBillDetailList = new ArrayList<>();
        initBasicFee(carBillDetailList, expressCarBillInfo.getBasicFeeDetail());
        initMainFee(carBillDetailList, expressCarBillInfo.getMainFeeDetailList());
        initOtherFee(carBillDetailList, expressCarBillInfo.getOtherFeeDetailList());
        initDiscountFee(carBillDetailList, expressCarBillInfo.getDiscountFee());
        bill.setCarBillDetailList(carBillDetailList);
        return bill;
    }

    private void initDiscountFee(List<ExpressCarBillDetail> carBillDetailList, String discountFee) {
        if (!TextUtils.isEmpty(discountFee) && !(Double.parseDouble(discountFee) == 0)) {
            ExpressCarBillDetail billDetail = new ExpressCarBillDetail();
            billDetail.setBillName("优惠额度")
                    .setBillDescribe("- ¥" + discountFee);
            carBillDetailList.add(billDetail);
        }
    }

    private void initOtherFee(List<ExpressCarBillDetail> carBillDetailList, List<OtherFeeDetail> otherFeeDetailList) {
        if (!CollectionUtils.isEmpty(otherFeeDetailList)) {
            for (OtherFeeDetail otherFeeDetail : otherFeeDetailList) {
                ExpressCarBillDetail billDetail = new ExpressCarBillDetail();
                billDetail.setBillName(otherFeeDetail.getName())
                        .setBillDescribe("¥" + otherFeeDetail.getAmount());
                carBillDetailList.add(billDetail);
            }
        }
    }

    private void initMainFee(List<ExpressCarBillDetail> carBillDetailList, List<MainFeeDetail> mainFeeDetailList) {
        if (!CollectionUtils.isEmpty(mainFeeDetailList)) {
            for (MainFeeDetail mainFeeDetail : mainFeeDetailList) {
                ExpressCarBillDetail billDetail = new ExpressCarBillDetail();
                if (mainFeeDetail.getType() == 0) {
                    billDetail.setBillName("里程费")
                            .setBillDescribe("¥"
                                    + mainFeeDetail.getAmount()
                                    + "(" + mainFeeDetail.getDetail() + "公里）");
                    carBillDetailList.add(billDetail);
                } else {
                    billDetail.setBillName("时长费")
                            .setBillDescribe("¥"
                                    + mainFeeDetail.getAmount()
                                    + "(" + mainFeeDetail.getDetail() + "分钟）");
                    carBillDetailList.add(billDetail);
                }
            }
        }
    }

    private void initBasicFee(List<ExpressCarBillDetail> carBillDetailList, BasicFeeDetail basicFeeDetail) {
        ExpressCarBillDetail billDetail = new ExpressCarBillDetail();
        billDetail.setBillName("起步价")
                .setBillDescribe("¥" + basicFeeDetail.getAmount()
                        + " (" + basicFeeDetail.getDuration() + "分钟 "
                        + basicFeeDetail.getMileage() + "公里）");
        carBillDetailList.add(billDetail);
    }
}
